/*     */ package dm.jdbc.innerData;
/*     */ 
/*     */ import dm.jdbc.dataConvertion.Convertion;
/*     */ import dm.jdbc.dbaccess.DBError;
/*     */ import java.sql.SQLException;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DmdbBdta
/*     */ {
/*     */   public static final int BDTA3_PACKAGE_NROWS = 0;
/*     */   public static final int BDTA3_PACKAGE_NFLDS = 4;
/*     */   public static final int BDTA3_PACKAGE_LENGTH = 6;
/*     */   public static final int BDTA3_PACKAGE_ORG_LEN = 10;
/*     */   public static final int BDTA3_PACKAGE_COMPRESS = 14;
/*     */   public static final int BDTA3_PACKAGE_HEAD_LEN = 15;
/*     */   private static final int VAR_DATA_LEN_STR = -2;
/*     */   private static final int VAR_DATA_LEN_DEC = -3;
/*     */   public int len;
/*     */   public int rownum;
/*     */   private int nflds;
/*     */   private int[] col_sql_type;
/*     */   private int[] col_offset;
/*     */   private int[] row_offset;
/*     */   private boolean[] col_is_all_not_null;
/*     */   private int curpos;
/*     */   private byte[] rowbuf;
/*  48 */   private int start_offset = 0;
/*     */   
/*     */   public DmdbBdta(byte[] rowbuf, int offset) throws SQLException {
/*  51 */     this.rowbuf = rowbuf;
/*  52 */     this.start_offset = offset;
/*  53 */     bdta3_unpack();
/*     */   }
/*     */   
/*     */   public void bdta3_unpack() throws SQLException {
/*  57 */     int offset = this.start_offset;
/*  58 */     byte[] buffer = this.rowbuf;
/*     */     
/*  60 */     this.len = ((int)Convertion.getUInt(buffer, offset + 6));
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  66 */     this.rownum = ((int)Convertion.getUInt(buffer, offset + 0));
/*     */     
/*  68 */     this.nflds = Convertion.getUShort(buffer, offset + 4);
/*     */     
/*  70 */     offset += 15;
/*     */     
/*     */ 
/*  73 */     this.col_sql_type = new int[this.nflds];
/*  74 */     for (int i = 0; i < this.nflds; i++) {
/*  75 */       this.col_sql_type[i] = Convertion.getUShort(buffer, offset);
/*  76 */       offset += 2;
/*     */     }
/*     */     
/*     */ 
/*  80 */     this.col_offset = new int[this.nflds];
/*  81 */     this.row_offset = new int[this.nflds];
/*  82 */     this.col_is_all_not_null = new boolean[this.nflds];
/*     */     
/*  84 */     for (int i = 0; i < this.nflds; i++) {
/*  85 */       this.col_offset[i] = 
/*  86 */         (this.start_offset + (int)Convertion.getUInt(buffer, offset));
/*  87 */       offset += 4;
/*     */       
/*     */ 
/*  90 */       int tmp_off = this.col_offset[i];
/*  91 */       this.col_is_all_not_null[i] = (Convertion.getUInt(buffer, tmp_off) == 1L ? 1 : 
/*  92 */         false);
/*  93 */       tmp_off += 4;
/*     */       
/*  95 */       if (this.col_is_all_not_null[i] == 0) {
/*  96 */         tmp_off += this.rownum * 1;
/*     */       }
/*  98 */       this.row_offset[i] = tmp_off;
/*     */     }
/*     */   }
/*     */   
/*     */   public boolean next() throws SQLException
/*     */   {
/* 104 */     if (this.curpos >= this.rownum - 1) {
/* 105 */       return false;
/*     */     }
/*     */     
/* 108 */     for (int colIndex = 0; colIndex < this.nflds; colIndex++) {
/* 109 */       if (!isNull(this.curpos, colIndex)) {
/* 110 */         int dtype_data_len = get_dtype_data_len(this.col_sql_type[colIndex]);
/* 111 */         if (dtype_data_len == -2)
/*     */         {
/* 113 */           int offset = this.row_offset[colIndex] + 4;
/*     */           
/* 115 */           int data_len = (int)Convertion.getUInt(this.rowbuf, offset);
/* 116 */           offset += 4;
/*     */           
/*     */ 
/* 119 */           this.row_offset[colIndex] = (offset + data_len);
/*     */         }
/* 121 */         else if (dtype_data_len == -3) {
/* 122 */           int data_len = (int)Convertion.getUInt(this.rowbuf, 
/* 123 */             this.row_offset[colIndex]);
/* 124 */           this.row_offset[colIndex] += data_len + 4;
/*     */         } else {
/* 126 */           this.row_offset[colIndex] += dtype_data_len;
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 132 */     this.curpos += 1;
/*     */     
/* 134 */     return true;
/*     */   }
/*     */   
/*     */   private boolean isNull(int rowIndex, int colIndex) {
/* 138 */     if (this.col_is_all_not_null[colIndex] != 0) {
/* 139 */       return false;
/*     */     }
/* 141 */     int temp = this.col_offset[colIndex] + 4 + rowIndex * 
/* 142 */       1;
/* 143 */     if (this.rowbuf[temp] == 1) {
/* 144 */       return false;
/*     */     }
/* 146 */     return true;
/*     */   }
/*     */   
/*     */   private int get_dtype_data_len(int dtype) throws SQLException {
/* 150 */     int datalen = 0;
/* 151 */     switch (dtype) {
/*     */     case 3: 
/*     */     case 5: 
/*     */     case 6: 
/*     */     case 7: 
/*     */     case 13: 
/*     */     case 25: 
/* 158 */       datalen = 4;
/* 159 */       break;
/*     */     
/*     */     case 8: 
/*     */     case 24: 
/* 163 */       datalen = 8;
/* 164 */       break;
/*     */     
/*     */     case 0: 
/*     */     case 1: 
/*     */     case 2: 
/*     */     case 12: 
/*     */     case 17: 
/*     */     case 18: 
/*     */     case 19: 
/* 173 */       datalen = -2;
/* 174 */       break;
/*     */     
/*     */     case 9: 
/* 177 */       datalen = -3;
/* 178 */       break;
/*     */     
/*     */     case 10: 
/* 181 */       datalen = 4;
/* 182 */       break;
/*     */     
/*     */     case 11: 
/* 185 */       datalen = 8;
/* 186 */       break;
/*     */     
/*     */     case 14: 
/*     */     case 15: 
/*     */     case 16: 
/*     */     case 22: 
/*     */     case 23: 
/* 193 */       datalen = 12;
/* 194 */       break;
/*     */     
/*     */     case 20: 
/* 197 */       datalen = 12;
/* 198 */       break;
/*     */     
/*     */     case 21: 
/* 201 */       datalen = 24;
/* 202 */       break;
/*     */     case 4: 
/*     */     default: 
/* 205 */       DBError.throwSQLException(6016);
/*     */     }
/*     */     
/* 208 */     return datalen;
/*     */   }
/*     */   
/*     */   public boolean absolute(int position) throws SQLException {
/* 212 */     if (position < 0) {
/* 213 */       position = 0;
/*     */     }
/*     */     
/* 216 */     if (position >= this.rownum) {
/* 217 */       position = this.rownum - 1;
/*     */     }
/*     */     
/* 220 */     if ((position != 0) || (this.curpos != 0)) {
/* 221 */       absolute_row_offset(position);
/*     */     }
/* 223 */     this.curpos = position;
/*     */     
/* 225 */     return true;
/*     */   }
/*     */   
/*     */   private void absolute_row_offset(int position) throws SQLException {
/* 229 */     for (int colIndex = 0; colIndex < this.nflds; colIndex++) {
/* 230 */       int offset = this.col_offset[colIndex] + 4;
/*     */       
/* 232 */       byte[] null_arr = null;
/* 233 */       if (this.col_is_all_not_null[colIndex] == 0) {
/* 234 */         null_arr = new byte[this.rownum];
/* 235 */         System.arraycopy(this.rowbuf, offset, null_arr, 0, this.rownum);
/* 236 */         offset += this.rownum * 1;
/*     */       }
/* 238 */       if (position <= 0) {
/* 239 */         this.row_offset[colIndex] = offset;
/*     */       } else {
/* 241 */         this.row_offset[colIndex] = bdta3_unpack_col_offset(this.rowbuf, offset, 
/* 242 */           colIndex, null_arr, position);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private int bdta3_unpack_col_offset(byte[] buffer, int offset, int colIndex, byte[] null_arr, int rows)
/*     */     throws SQLException
/*     */   {
/* 250 */     int dtype_data_len = get_dtype_data_len(this.col_sql_type[colIndex]);
/* 251 */     if (dtype_data_len == -2) {
/* 252 */       offset = bdta3_unpack_col_offset_str(buffer, offset, 
/* 253 */         this.col_is_all_not_null[colIndex], null_arr, rows);
/* 254 */     } else if (dtype_data_len == -3) {
/* 255 */       offset = bdta3_unpack_col_offset_dec(buffer, offset, 
/* 256 */         this.col_is_all_not_null[colIndex], null_arr, rows);
/*     */     }
/* 258 */     else if (this.col_is_all_not_null[colIndex] != 0) {
/* 259 */       offset += rows * dtype_data_len;
/*     */     } else {
/* 261 */       int nullcount = 0;
/* 262 */       for (int j = 0; j < rows; j++) {
/* 263 */         if (null_arr[j] == 0)
/* 264 */           nullcount++;
/*     */       }
/* 266 */       offset += (rows - nullcount) * dtype_data_len;
/*     */     }
/*     */     
/* 269 */     return offset;
/*     */   }
/*     */   
/*     */   private int bdta3_unpack_col_offset_str(byte[] buffer, int offset, boolean is_all_not_null, byte[] null_arr, int rowcount)
/*     */   {
/* 274 */     int data_len = 0;
/* 275 */     if (is_all_not_null) {
/* 276 */       for (int i = 0; i < rowcount; i++)
/*     */       {
/* 278 */         offset += 4;
/*     */         
/*     */ 
/* 281 */         data_len = (int)Convertion.getUInt(buffer, offset);
/* 282 */         offset += 4;
/*     */         
/*     */ 
/* 285 */         offset += data_len;
/*     */       }
/*     */     } else {
/* 288 */       for (int i = 0; i < rowcount; i++) {
/* 289 */         if (null_arr[i] == 1)
/*     */         {
/* 291 */           offset += 4;
/*     */           
/*     */ 
/* 294 */           data_len = (int)Convertion.getUInt(buffer, offset);
/* 295 */           offset += 4;
/*     */           
/*     */ 
/* 298 */           offset += data_len;
/*     */         }
/*     */       }
/*     */     }
/* 302 */     return offset;
/*     */   }
/*     */   
/*     */   private int bdta3_unpack_col_offset_dec(byte[] buffer, int offset, boolean is_all_not_null, byte[] null_arr, int rowcount)
/*     */   {
/* 307 */     int data_len = 0;
/* 308 */     if (is_all_not_null) {
/* 309 */       for (int i = 0; i < rowcount; i++)
/*     */       {
/* 311 */         data_len = (int)Convertion.getUInt(buffer, offset);
/* 312 */         offset += 4;
/*     */         
/*     */ 
/* 315 */         offset += data_len;
/*     */       }
/*     */     } else {
/* 318 */       for (int i = 0; i < rowcount; i++) {
/* 319 */         if (null_arr[i] == 1)
/*     */         {
/* 321 */           data_len = (int)Convertion.getUInt(buffer, offset);
/* 322 */           offset += 4;
/*     */           
/*     */ 
/* 325 */           offset += data_len;
/*     */         }
/*     */       }
/*     */     }
/* 329 */     return offset;
/*     */   }
/*     */   
/*     */   public boolean previous() throws SQLException {
/* 333 */     if (this.curpos <= 0) {
/* 334 */       return false;
/*     */     }
/*     */     
/* 337 */     previous_row_offset();
/* 338 */     this.curpos -= 1;
/*     */     
/* 340 */     return true;
/*     */   }
/*     */   
/*     */   private void previous_row_offset() throws SQLException {
/* 344 */     for (int colIndex = 0; colIndex < this.nflds; colIndex++) {
/* 345 */       if (!isNull(this.curpos - 1, colIndex)) {
/* 346 */         int dtype_data_len = get_dtype_data_len(this.col_sql_type[colIndex]);
/* 347 */         if ((dtype_data_len == -2) || 
/* 348 */           (dtype_data_len == -3)) {
/* 349 */           int offset = this.col_offset[colIndex] + 4;
/*     */           
/* 351 */           byte[] null_arr = null;
/* 352 */           if (this.col_is_all_not_null[colIndex] == 0) {
/* 353 */             null_arr = new byte[this.rownum];
/* 354 */             System.arraycopy(this.rowbuf, offset, null_arr, 0, this.rownum);
/* 355 */             offset += this.rownum * 1;
/*     */           }
/*     */           
/* 358 */           if (dtype_data_len == -2) {
/* 359 */             this.row_offset[colIndex] = bdta3_unpack_col_offset_str(
/* 360 */               this.rowbuf, offset, this.col_is_all_not_null[colIndex], 
/* 361 */               null_arr, this.curpos - 1);
/*     */           } else
/* 363 */             this.row_offset[colIndex] = bdta3_unpack_col_offset_dec(
/* 364 */               this.rowbuf, offset, this.col_is_all_not_null[colIndex], 
/* 365 */               null_arr, this.curpos - 1);
/*     */         } else {
/* 367 */           this.row_offset[colIndex] -= dtype_data_len;
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public byte[] getBytes(short columnNo) throws SQLException
/*     */   {
/* 375 */     if (isNull(this.curpos, columnNo)) {
/* 376 */       return null;
/*     */     }
/* 378 */     int data_len = get_dtype_data_len(this.col_sql_type[columnNo]);
/* 379 */     int offset = this.row_offset[columnNo];
/* 380 */     if (data_len == -2) {
/* 381 */       offset += 4;
/* 382 */       data_len = (int)Convertion.getUInt(this.rowbuf, offset);
/* 383 */       offset += 4;
/* 384 */     } else if (data_len == -3) {
/* 385 */       data_len = (int)Convertion.getUInt(this.rowbuf, offset);
/* 386 */       offset += 4;
/*     */     }
/*     */     
/* 389 */     byte[] value = new byte[data_len];
/* 390 */     System.arraycopy(this.rowbuf, offset, value, 0, data_len);
/*     */     
/* 392 */     return value;
/*     */   }
/*     */   
/*     */   public long rec_get_rowid(int rowidCol) {
/*     */     try {
/* 397 */       byte[] rowid_bytes = getBytes((short)rowidCol);
/* 398 */       return Convertion.bytesToLong(rowid_bytes);
/*     */     }
/*     */     catch (SQLException e) {
/* 401 */       e.printStackTrace(); }
/* 402 */     return -1L;
/*     */   }
/*     */ }


/* Location:              D:\Program Files\DBMS_20150813\jdbc\Dm7JdbcDriver17.jar!\dm\jdbc\innerData\DmdbBdta.class
 * Java compiler version: 2 (46.0)
 * JD-Core Version:       0.7.1
 */